home *** CD-ROM | disk | FTP | other *** search
- Path: news.iag.net!news
- From: jatmon@iag.net (John R Buchan)
- Newsgroups: comp.lang.c
- Subject: Re: What the hell is THIS?!
- Date: 14 Jan 1996 19:51:46 GMT
- Organization: Internet Access Group, Orlando, Florida
- Message-ID: <4dbmsi$47c@news.iag.net>
- References: <4d6rgh$rfu@abel.cc.sunysb.edu> <4d7g6u$j0l@umbc9.umbc.edu>
- NNTP-Posting-Host: pm3-orl13.iag.net
- X-Newsreader: WinVN 0.99.7
-
- In article <4d7g6u$j0l@umbc9.umbc.edu>, schlein@umbc.edu says...
- >
- >Bommasamudram Madhusudan <bmadhusu@engws12.ic.sunysb.edu> wrote:
- >|> hi guys;
- >|>
- >|> I'm going nuts trying to figure this out;
- >|>
- >|> Can someone explain what
- >|>
- >|> int (*p)[3] is?????
- >|>
- >|> Is this an array of pointers to integers
- >|>
- >|> OR
- >|>
- >|> a pointer to an array of integers?? HELP!
- >
- >According to cdecl:
- >
- >cdecl> explain int (*p)[3]
- >declare p as pointer to array 3 of int
- >
- >|> I can say things like:
- >|>
- >|> (*p)[0] = 3; for e.g, but when I print the value using:
- >|>
- >|> printf("%d",(*p)[0]) I get a core dump!
- >
- >Exactly...Because it is a pointer, you need to associate some space
- >with it via malloc() or the like.
-
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
-
- int main(void)
- {
- int (*p)[3], ary[4][3];
-
- p = ary; /* p points to ary[0][0] */
- p++; /* p points to ary[1][0] */
-
- p[0][2] = 3; /* ary[1][2] now == 3 */
-
- p = malloc( 3 * sizeof(*p)); /* same as sizeof(int(*)[3]) */
- if(p == NULL)
- exit(EXIT_FAILURE);
-
- /* p now points to a dynamic 3x3 array of ints */
- p[1][2] = 2;
- printf( "%d\n", p[1][2]); /* this will output 2 */
- free(p);
-
- return (0);
- }
-
- The c.l.c faq list has some information on this. It is available for
- anonymous ftp from rtfm.mit.edu /pub/usenet/comp.lang.c.
-
-
- --
- John R Buchan -:|:- Looking for that elusive FAQ? ftp to:
- jatmon@mail.iag.net -:|:- rtfm.mit.edu /pub/usenet-by-group/....
-
-